# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import *
from sqlalchemy.orm import *
from Environment import *
from web_lib.webutils import leggiInSessione

schemaa = leggiInSessione(sezione="Main", chiave="schema")
try:
    t_categoria = Table('categoria', meta, schema=schemaa, autoload=True)
    t_ente = Table('ente', meta, schema=mainSchema, autoload=True)
    t_entecategoria=Table('ente_categoria', meta, schema=schemaa, autoload=True)
except:
    #from data.action import t_action
    from data.categoria import t_categoria
    from data.ente import t_ente
    from data.enteCategoria import t_entecategoria

from dao.Dao import Dao
from dao.Ente import Ente
from dao.Categoria import Categoria

class EnteCategoria(Dao):
    """ RoleAction class database functions  """
    def __init__(self, req=None):
        Dao.__init__(self, entity=self)

    def filter_values(self,k,v):
        if k == 'id_ente':
            dic = {k:t_entecategoria.c.id_ente == v}
        elif k == 'id_categoria':
            dic = {k:t_entecategoria.c.id_categoria == v}
        return  dic[k]

    @property
    def ente(self):
        if self.id_ente:
            a = Ente().getRecord(id=self.id_ente)
            if a:
                return a
            else:
                return None
    @property
    def categoria(self):
        if self.id_categoria:
            a = Categoria().getRecord(id=self.id_categoria)
            if a:
                return a
            else:
                return None

std_mapper = mapper(EnteCategoria, t_entecategoria, properties={
            #'ente':relation(Ente, backref='entecategoria'),
            #'categoria':relation(Categoria, backref='entecategoria'),
                }, order_by=t_entecategoria.c.id_ente)
